﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

using System.Web;
using LILI.Tools;
using LILI.Data.Model;
namespace LILI.Data.DAL
{
    public class D_Users
    {
        #region 插入用户类型
        /// <summary>
        /// 插入用户类型
        /// </summary>
        /// <param name="userTypeName"></param>
        /// <returns></returns>
        public static int InserUserType(string userTypeName)
        {
            SqlParameter[] parameters = {
                  new SqlParameter("@userTypeName", SqlDbType.VarChar),
                                         };
            parameters[0].Value = userTypeName;
            string strSql = string.Format("INSERT INTO User_Type(UserTypeName)VALUES(@userTypeName);select @@IDENTITY");
            return DataHelper.ExecuteScalar<int>(strSql, parameters);
        }
        #endregion

        #region 插入用户
        /// <summary>
        /// 插入用户
        /// </summary>
        public static int InsertUser(string userName, string passWord, int userType, string userEmail)
        {
            SqlParameter[] parameters ={
                           new SqlParameter("@userName",SqlDbType.NVarChar),
                           new SqlParameter("@passWord",SqlDbType.VarChar),
                           new SqlParameter("@userType",SqlDbType.Int),
                           new SqlParameter("@userEmail",SqlDbType.VarChar)
                                      };
            parameters[0].Value = userName;
            parameters[1].Value = SecurityUtils.GetMd5Str(passWord);
            parameters[2].Value = userType;
            parameters[3].Value = userEmail;
            string strSql = string.Format("INSERT INTO User_Info(UserName,UserPwd,UserTypeID,UserEmail)VALUES(@userName,@passWord,@userType,@userEmail);select @@IDENTITY");
            return DataHelper.ExecuteScalar<int>(strSql, parameters);
        }
        #endregion

        #region 获取当前的用户ID
        /// <summary>
        /// 获取当前的用户ID
        /// </summary>
        public static int? CurrentUserID
        {
            get
            {
                if (HttpContext.Current.Session["UserID"] == null)
                {
                    return null;
                }
                else
                {
                    return int.Parse(HttpContext.Current.Session["UserID"].ToString());
                }
            }
        }
        #endregion

        /// <summary>
        /// 判断用户名是否注册
        /// </summary>
        /// <param name="userName"></param>
        /// <returns>注册返回true;未注册返回false</returns>
        public static bool ExistsUser(string userName)
        {
            SqlParameter[] parameters = 
            {
               new SqlParameter("@userName",SqlDbType.NVarChar)
            };
            parameters[0].Value = userName;
            string strSql = string.Format("select count(1) from User_Info where UserName=@userName");
            int i = DataHelper.ExecuteScalar<int>(strSql, parameters);
            return i > 0 ? true : false;
        }
        /// <summary>
        /// 返回用户信息实体
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="passWord"></param>
        /// <returns></returns>
        public static IList<M_Users> GetUsersList(string userName, string passWord)
        {
            SqlParameter[] parameters = 
            {
                new SqlParameter("@userName",SqlDbType.NVarChar),
                new SqlParameter("@passWord",SqlDbType.VarChar)
             };
            parameters[0].Value = userName;
            parameters[1].Value = SecurityUtils.GetMd5Str(passWord);
            StringBuilder strSql = new System.Text.StringBuilder();
            strSql.Append("SELECT ui.UserID, ui.UserName, ui.UserPwd, ui.UserTypeID, ui.UserEmail");
            strSql.Append("FROM User_Info ui WHERE ui.UserName=@userName AND ui.UserPwd=@passWord");
            SqlDataReader sqlDataReader = DataHelper.ExecuteReader(strSql.ToString(),parameters);
            return sqlDataReader.ConvertToList<M_Users>(true);
        }
    }
}
